<template>
    <div class="login">
        <van-nav-bar
            title="登 录"
            left-text="返回"
            left-arrow
            @click-left="$router.go(-1)"
        />
        <div class="submit">
            <van-form @submit="onSubmit">
                <div class="user">
                    <van-field
                        v-model="username"
                        name="username"
                        label="用户名"
                        placeholder="用户名"
                    />
                </div>
                <div class="user">
                    <van-field
                        v-model="password"
                        type="password"
                        name="password"
                        label="密码"
                        placeholder="密码"
                    />
                </div>
                <div style="margin: 16px;">
                    <van-button round block type="info" native-type="submit">提交</van-button>
                </div>
            </van-form>
        </div>
        <p class="login-reg" @click="$router.push('/reg')">没有账号？去往注册</p>
    </div>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            username: "",
            password: ""
        };
    },
    props: {},
    methods: {
        async onSubmit(values) {
            // console.log(values.username,values.password)
            let res = await this.$axios({
                url:'/api/login',
                method: 'post',
                data:{
                    username: values.username,
                    password: values.password
                }
            });
            // console.log(this.$route.query.p)
            if (!res.data.err) {
                window.localStorage.setItem('user',JSON.stringify(res.data));
                this.$router.push(this.$route.query.p)
            } else {
                alert('账号或密码错误')
            }
        }
    },
    components: {},
    mounted() {},
    updated() {}
};
</script>

<style scoped>
.submit {
    margin: 30px 10px;
}
.user {
    margin: 20px 0;
}
.login-reg{
    font-size: 16px;
    text-align: center;
    color: rgb(68, 221, 221);
    cursor: pointer;
}
</style>
